package com.yuncheng.spcyApi.spcy;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.spcyApi.alog.annotation.Log;
import com.yuncheng.spcyApi.bo.JcyjParamBo;
import com.yuncheng.spcyApi.busService.YwlxmbService;
import com.yuncheng.spcyApi.constant.other.YwlxmbConstant;
import com.yuncheng.spcyApi.entity.SpcyRsWjmb;
import com.yuncheng.spcyApi.entity.SpcyRsWjmbZjml;
import com.yuncheng.spcyApi.entity.SpcySqsxBasic;
import com.yuncheng.spcyApi.entity.SpcySqsxYwlxmb;
import com.yuncheng.spcyApi.service.ISpcyRsWjmbService;
import com.yuncheng.spcyApi.service.ISpcyRsWjmbZjmlService;
import com.yuncheng.spcyApi.service.ISpcySqsxBaiscService;
import com.yuncheng.spcyApi.utils.GetCurrentUserUtils;
import com.yuncheng.spcyApi.vo.wjmb.WjmbJcxmTitleVo;
import com.yuncheng.vo.HttpResult;
import com.yuncheng.spcyApi.vo.common.HttpResultVo;
import com.yuncheng.spcyApi.vo.common.Result;
import com.yuncheng.spcyApi.vo.sqsx.SqsxYwlxmbVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author hua
 */
@Api(tags = "现场检查-整体及章节模板接口")
@RestController
@RequestMapping("/api/spzx-spcy/jcmb")
public class SpcyXcjcMbController {

    @Resource
    private YwlxmbService ywlxmbService;

    @Resource
    private ISpcyRsWjmbZjmlService spcyRsWjmbZjmlService;

    @Resource
    private ISpcyRsWjmbService spcyRsWjmbService;

    @Resource
    private ISpcySqsxBaiscService spcySqsxBaiscService;

    @Log(title = "现场检查-提取模板")
    @ApiOperation(value = "现场检查-提取模板")
    @GetMapping(value = "tqmb")
    public HttpResult tqmb(@RequestParam(value = "sqsxid",defaultValue = "")String sqsxid,
                           @RequestParam(value = "type",defaultValue = "")String type,
                           @RequestParam(value = "fmbbt",defaultValue = "")String fmbbt){
        if (StringUtils.isBlank(sqsxid)){
            return HttpResult.error("参数为空！");
        }

        if (StringUtils.isBlank(type)){
            return HttpResult.error("模板参数为空！");
        }

        SpcySqsxBasic sqsx = spcySqsxBaiscService.selectSqsxById(sqsxid);
        if (sqsx == null){
            return HttpResult.error("没有相匹配的事项！");
        }

        int order = 1;
        List<SqsxYwlxmbVo> resultDataList = new ArrayList<>();

        if (StringUtils.isNotBlank(type)){

            WjmbJcxmTitleVo wjmbJcxmTitleVo = spcyRsWjmbService.selectJcxmTitleByFsqsxId(sqsxid);
            if (!type.contains(wjmbJcxmTitleVo.getFmsmbbt())) {
                List<SpcyRsWjmbZjml> mbZjList = new ArrayList<>();
                /** 是否为合并事项 */
                if (StringUtils.isNotBlank(sqsx.getFhbsxids())) {
                    mbZjList = spcyRsWjmbZjmlService.selectZjByHbsxId(sqsx.getFhbsxids(), sqsxid);
                } else {
                    mbZjList = spcyRsWjmbZjmlService.selectWjmbZjmlList(sqsxid);
                }

                String filterType = "";
                if (type.contains("、")){
                    filterType = type.split("、")[1];
                }else {
                    filterType = type;
                }

                String finalFilterType = filterType;

                List<SpcyRsWjmbZjml> filterTypeList = mbZjList.stream()
                        .filter(f -> StringUtils.isNotBlank(f.getFzjmc())
                        && f.getFzjmc().contains(finalFilterType))
                        .collect(Collectors.toList());

                if (StringUtils.isNotBlank(fmbbt) && CollectionUtil.isNotEmpty(filterTypeList)){
                    filterTypeList = filterTypeList.stream()
                            .filter(f -> StringUtils.isNotBlank(f.getFzjmc())
                                    && f.getFzjmc().contains(fmbbt))
                            .collect(Collectors.toList());
                }

                for (SpcyRsWjmbZjml mb : filterTypeList){
                    SqsxYwlxmbVo newMb = new SqsxYwlxmbVo();
                    newMb.setForder(order);
                    newMb.setFmbly(YwlxmbConstant.MBLY_SPZX);
                    newMb.setFmbbt(mb.getFzjmc());
                    newMb.setFmbnr(mb.getFmsmbnr());

                    resultDataList.add(newMb);

                    order++;
                }

            }else {

                List<SpcyRsWjmb> mbList = new ArrayList<>();
                /** 是否为合并事项 */
                if (StringUtils.isNotBlank(sqsx.getFhbsxids())) {
                    mbList = spcyRsWjmbService.list(new LambdaQueryWrapper<SpcyRsWjmb>()
                            .eq(SpcyRsWjmb::getFsqsxid, sqsx.getFhbsxids())
                            .like(SpcyRsWjmb::getFmblx3, sqsxid));
                }else {
                    mbList = spcyRsWjmbService.list(new LambdaQueryWrapper<SpcyRsWjmb>()
                            .eq(SpcyRsWjmb::getFsqsxid, sqsxid));
                }

                if (StringUtils.isNotBlank(fmbbt) && CollectionUtil.isNotEmpty(mbList)){
                    mbList = mbList.stream()
                            .filter(f -> StringUtils.isNotBlank(f.getFmbbt())
                                    && f.getFmbbt().contains(fmbbt))
                            .collect(Collectors.toList());
                }

                if (CollectionUtil.isNotEmpty(mbList)){
                    SqsxYwlxmbVo newMb = new SqsxYwlxmbVo();
                    newMb.setId("ztms");
                    newMb.setForder(order);
                    newMb.setFmbly(YwlxmbConstant.MBLY_SPZX);
                    newMb.setFmbbt(mbList.get(0).getFmbbt());
                    newMb.setFmbnr(mbList.get(0).getFmsmbnr());

                    resultDataList.add(newMb);

                    order++;
                }
            }
        }

        List<SpcySqsxYwlxmb> commonMbList = ywlxmbService.selectCommonMbList(sqsx.getFywflcode(), GetCurrentUserUtils.getCurrentUserId());

        if (StringUtils.isNotBlank(fmbbt) && CollectionUtil.isNotEmpty(commonMbList)){
            commonMbList = commonMbList.stream()
                    .filter(f -> StringUtils.isNotBlank(f.getFmbbt())
                            && f.getFmbbt().contains(fmbbt))
                    .collect(Collectors.toList());
        }

        for (SpcySqsxYwlxmb mb : commonMbList){
            SqsxYwlxmbVo newMb = new SqsxYwlxmbVo();
            BeanUtil.copyProperties(mb, newMb);
            newMb.setForder(order);
            resultDataList.add(newMb);

            order++;
        }


        return HttpResultVo.HttpResultList(resultDataList);
    }

    @Log(title = "新增为个人模板")
    @ApiOperation(value = "新增为个人模板")
    @PostMapping(value = "addmb")
    public HttpResult addmb(@RequestBody JcyjParamBo paramBo){
        if (StringUtils.isBlank(paramBo.getSqsxid())){
            return HttpResult.error("事项参数为空！");
        }

        if (StringUtils.isBlank(paramBo.getFmbbt())){
            return HttpResult.error("标题为空！");
        }

        if (StringUtils.isBlank(paramBo.getFmbnr())){
            return HttpResult.error("内容为空！");
        }

        SpcySqsxBasic sqsx = spcySqsxBaiscService.selectSqsxYwflCodeById(paramBo.getSqsxid());
        if (sqsx == null){
            return HttpResult.error("当前事项数据不存在！");
        }

        ywlxmbService.addCommonMb(paramBo, sqsx.getFywflcode());

        return HttpResult.ok("保存成功");
    }

    @Log(title = "更新个人模板")
    @ApiOperation(value = "更新个人模板")
    @PostMapping("updateMb")
    public HttpResult updateMb(@RequestBody HashMap<String,Object> parVo){

        Result result = ywlxmbService.updateCommonMb(parVo);
        if (!result.isSuccess()){
            return HttpResult.error(result.getMessage());
        }

        return HttpResult.ok("保存成功");
    }

    @Log(title = "删除个人模板")
    @ApiOperation(value = "删除个人模板")
    @DeleteMapping(value = "deleteMb")
    public HttpResult deleteMb(@RequestParam(value = "id",defaultValue = "")String id){
        if (StringUtils.isBlank(id)){
            return HttpResult.error("参数为空！");
        }

        ywlxmbService.deleteYwlxmb(id);
        return HttpResult.ok("删除成功");
    }



}
